//
//  ARTWatchSleepTextView.m
//  ChartsDemo
//
//  Created by yunda on 2022/5/25.
//

#import "ARTWatchSleepTextView.h"

@implementation ARTWatchSleepTextView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }
    self.backgroundColor = [UIColor whiteColor];
    J_ViewRadius(self, 10);
    [self addSubview:self.contentLabel];
    return self;
}

- (void)configureUI{
    NSMutableAttributedString *contentAttribute = [[NSMutableAttributedString alloc]initWithString:_title attributes:@{
        NSFontAttributeName:J_Font(14),
        NSForegroundColorAttributeName:J_ColorFromHEX(0x222222)
    }];
    
    [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]];
    [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:_content attributes:@{
        NSFontAttributeName:J_Font(12),
        NSForegroundColorAttributeName:J_ColorFromHEX(0x666666)
    }]];
    
    if (_subContent.length > 0) {
        [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]];
        [contentAttribute appendAttributedString:[[NSAttributedString alloc] initWithString:_subContent attributes:@{
            NSFontAttributeName:J_Font(10),
            NSForegroundColorAttributeName:J_ColorFromHEX(0x999999)
        }]];
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = 4;
    [contentAttribute addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:contentAttribute.rangeOfAll];
    _contentLabel.attributedText = contentAttribute;
    
    CGFloat x = 15;
    YYTextContainer  *titleContarer = [YYTextContainer new];
    titleContarer.size = CGSizeMake(self.v_w-x*2,CGFLOAT_MAX);
    YYTextLayout *titleLayout = [YYTextLayout layoutWithContainer:titleContarer text:contentAttribute];
    CGSize size = titleLayout.textBoundingSize;
    _contentLabel.frame = J_Frame(15, 22, size.width, size.height);
    self.v_h = _contentLabel.v_MaxY+22;
    
//    if (self.heightChangedBlock) {
//        self.heightChangedBlock();
//    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (YYLabel *)contentLabel{
    if (_contentLabel == nil) {
        _contentLabel = [[YYLabel alloc] init];
        _contentLabel.displaysAsynchronously = YES;
        _contentLabel.numberOfLines = 0;
        _contentLabel.backgroundColor = [UIColor whiteColor];
        _contentLabel.font = J_Font(14);
    }
    return _contentLabel;
}

@end
